import 'package:flappy_search_bar/flappy_search_bar.dart';
import 'package:flappy_search_bar/search_bar_style.dart';
import 'package:flutter/material.dart';

class Post {
  final String title;
  final String description;

  Post(this.title, this.description);
}

class ContentSearchBar extends StatelessWidget {
  final Function onCancel;

  ContentSearchBar(this.onCancel);

  Future<List<Post>> search(String search) async {
    await Future.delayed(Duration(milliseconds: 100));
    if (search == "empty") return [];
    if (search == "error") throw Error();
    return List.generate(search.length, (int index) {
      return Post(
        "Title : $search $index",
        "Description :$search $index",
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Row(
        children: [
          SizedBox(
            width: 20,
          ),
          Expanded(
            child: SearchBar<Post>(
              loader: Text("loading..."),
              icon: Container(
                margin: EdgeInsets.only(left: 10),
                child: Icon(Icons.search),
              ),
              searchBarStyle: SearchBarStyle(
                padding: EdgeInsets.all(1),
                borderRadius: BorderRadius.circular(30),
              ),
              hintText: '搜索卡书',
              searchBarPadding: EdgeInsets.symmetric(horizontal: 0),
//            placeHolder: Text("placeholder"),
//              mainAxisSpacing: 10,
//              crossAxisSpacing: 10,
              crossAxisCount: 1,
              iconActiveColor: Colors.blue,
              cancellationWidget: Text("取消"),
              onCancelled: () {
                onCancel();
              },
              suggestions: [
              ],
              onSearch: search,
              onItemFound: (Post post, int index) {
                return ListTile(
                  title: Text(post.title),
                  subtitle: Text(post.description),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
